package com.yiqi.lottery.feature.mine.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.PowerfulEditText;
import com.yiqi.lottery.R;
import com.yiqi.lottery.feature.login.view.LoginActivity;
import com.yiqi.lottery.feature.mine.contract.BindContract;
import com.yiqi.lottery.feature.mine.dependencies.bind.BindPresenterModule;
import com.yiqi.lottery.feature.mine.dependencies.bind.DaggerBindComponent;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

import javax.inject.Inject;
import java.util.concurrent.TimeUnit;

public class EditPhoneActivity extends BaseActivity implements BindContract.View {

    @Inject
    BindContract.Presenter mPresenter;

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.view_phone_first)
    LinearLayout mViewPhoneFirst;
    @BindView(R.id.view_phone_next)
    LinearLayout mViewPhoneNext;
    @BindView(R.id.view_phone_success)
    LinearLayout mViewPhoneSuccess;
    @BindView(R.id.tv_phone)
    TextView mTvPhone;
    @BindView(R.id.edt_user_edit_phone_code_old)
    PowerfulEditText mEdtUserEditPhoneCodeOld;
    @BindView(R.id.btn_user_editphone_code_old)
    Button mBtnUserEditphoneCodeOld;
    @BindView(R.id.edt_user_edit_phone_new)
    PowerfulEditText mEdtUserEditPhoneNew;
    @BindView(R.id.btn_user_editphone_code_new)
    Button mBtnUserEditphoneCodeNew;
    @BindView(R.id.edt_user_editphone_code_new)
    PowerfulEditText mEdtUserEditphoneCodeNew;
    @BindView(R.id.tv_user_edit_phone_old)
    TextView mTvUserEditPhoneOld;
    @BindView(R.id.tv_user_edit_phone_show_new)
    TextView mTvUserEditPhoneShowNew;
    @BindView(R.id.tv_show_success_phone)
    TextView mTvShowSuccessPhone;


    private  boolean isShowNewButton=false;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_edit_phone;
    }

    @Override
    protected void initInjector() {
        DaggerBindComponent.builder().bindPresenterModule(new BindPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "修改手机", true, true);
    }

    @Override
    protected void initData() {
        mTvUserEditPhoneOld.setText(AM.user().getLoginResultCache().getMobile());
    }


    @OnClick({R.id.btn_user_editphone_next, R.id.btn_user_editphone_comlete, R.id.btn_user_editphone_success,
            R.id.btn_user_editphone_code_old, R.id.btn_user_editphone_code_new})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_user_editphone_code_old://获取旧手机验证码
                mPresenter.doUserBindSendCode(mTvUserEditPhoneOld.getText().toString());
                break;
            case R.id.btn_user_editphone_next: //验证旧的手机号码

                mPresenter.doGetOldPhoneVerify(mTvUserEditPhoneOld.getText().toString(), mEdtUserEditPhoneCodeOld.getText().toString());
                break;
            case R.id.btn_user_editphone_code_new://获取新手机验证码
                mPresenter.doUserBindSendCode(mEdtUserEditPhoneNew.getText().toString());
                break;
            case R.id.btn_user_editphone_comlete:
                mPresenter.doGetNewPhoneVerify(mEdtUserEditPhoneNew.getText().toString(), mEdtUserEditphoneCodeNew.getText().toString());
                break;
            case R.id.btn_user_editphone_success:
                AM.user().saveLoginResultCache(null);
                startActivity(new Intent(this, LoginActivity.class));
                finish();
                break;
        }
    }


    @Override
    public void showOldPhoneVerifySuccess(String msg) {
        initToolBar(mToolbar, "验证新手机", true, true);
        mViewPhoneFirst.setVisibility(View.GONE);
        mViewPhoneNext.setVisibility(View.VISIBLE);
        isShowNewButton=true;
    }

    @Override
    public void showOldPhoneVerifyError(String msg) {
        T.showShort(msg);
    }

    @Override
    public void showNewPhoneVerifySuccess(String msg) {
        initToolBar(mToolbar, "修改手机", true, true);
        mViewPhoneFirst.setVisibility(View.GONE);
        mViewPhoneNext.setVisibility(View.GONE);
        mViewPhoneSuccess.setVisibility(View.VISIBLE);
        mTvShowSuccessPhone.setText(mEdtUserEditPhoneNew.getText().toString());
    }

    @Override
    public void showNewPhoneVerifyError(String msg) {
        T.showShort(msg);
    }

    @Override
    public void showSendCodeSuccess(String code) {
        T.showShort("验证码获取成功！");
        if (isShowNewButton){
            onGetCode2();
        }else {
            onGetCode();
        }

    }

    @Override
    public void showSendCodeError(String error) {
        T.showShort(error);
    }



    public void onGetCode() {
        final int count = 60;//倒计时10秒
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())//ui线程中进行控件更新
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnUserEditphoneCodeOld.setEnabled(false);
                        mBtnUserEditphoneCodeOld.setTextColor(AM.res().getColor(R.color.text_secondary));
                        mBtnUserEditphoneCodeOld.setBackgroundResource(R.drawable.common_normal_bg);
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                mBtnUserEditphoneCodeOld.setText( num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }
//
            @Override
            public void onComplete() {
                //回复原来初始状态
                mBtnUserEditphoneCodeOld.setEnabled(true);
                mBtnUserEditphoneCodeOld.setText("发送验证码");
                mBtnUserEditphoneCodeOld.setTextColor(AM.res().getColor(R.color.primary_comparison));
                mBtnUserEditphoneCodeOld.setBackgroundResource(R.drawable.common_primary_bg);
            }
        });

    }

    public void onGetCode2() {
        final int count = 60;
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(count + 1)
                .map(new Function<Long, Long>() {
                    @Override
                    public Long apply(Long aLong) throws Exception {
                        return count - aLong;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        mBtnUserEditphoneCodeNew.setEnabled(false);
                        mBtnUserEditphoneCodeNew.setTextColor(AM.res().getColor(R.color.text_secondary));
                        mBtnUserEditphoneCodeNew.setBackgroundResource(R.drawable.common_normal_bg);
                    }
                }).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Long num) {

                mBtnUserEditphoneCodeNew.setText( num + "s");
            }

            @Override
            public void onError(Throwable e) {

            }
            //
            @Override
            public void onComplete() {
                mBtnUserEditphoneCodeNew.setEnabled(true);
                mBtnUserEditphoneCodeNew.setText("发送验证码");
                mBtnUserEditphoneCodeNew.setTextColor(AM.res().getColor(R.color.primary_comparison));
                mBtnUserEditphoneCodeNew.setBackgroundResource(R.drawable.common_primary_bg);
            }
        });

    }
}
